enum DATE {

    //% block="P07"
    0,
    //% block="P06"
    1,
    //% block="P05"
    2,
    //% block="P04"
    3,
    //% block="P03"
    4,
    //% block="P02"
    5,
    //% block="P01"
    6,
    //% block="P00"
    7,
    //% block="P47"
    8,
    //% block="P46"
    9,
    //% block="P45"
    10,
    //% block="P44"
    11,
    //% block="P43"
    12,
    //% block="P42"
    13,
    //% block="P41"
    14,
    //% block="P40"
    15,
}
enum IO {
    //% block="P10"
    1,
    //% block="P11"
    2,
    //% block="P13"
    3,
    //% block="P14"
    4,
    //% block="P15"
    5,
    //% block="P16"
    6,
    //% block="P17"
    7,
    //% block="P20"
    8,
    //% block="P21"
    9,
    //% block="P22"
    10,
    //% block="P23"
    11,
    //% block="P26"
    12,
    //% block="P27"
    13,
    //% block="P52"
    14,

}
enum LEVEL{
    //% block="低电平"
    0,
    //% block="高电平"
    14,
}

enum LEN{
    //% block="P07-P00"
    1,
    //% block="P47-P40"
    2,
    //% block="全部"
    3,
}

enum STA{
    //% block="数字输入引脚状态"
    1,
    //% block="数字输出引脚状态"
    2,

}

enum PWM{
    //% block="P11"
    1,
    //% block="P13"
    2,
    //% block="P15"
    3,
    //% block="P17"
    4,
    //% block="P20"
    5,
    //% block="P21"
    6,
    //% block="P22"
    7,
    //% block="P23"
    8,
}


//% color="#FFC125" iconWidth=50 iconHeight=40
namespace I2C {

    function begin() {
        Generator.addInclude('XYXKC_I2C','#include <XYXKC_I2C.h>')
        Generator.addObject('I2C_ib','XYXKC_I2C','I2C_ib;')
        Generator.addSetup('I2CSetup',`I2C_ib.begin();`)

    }
    //% block="请求一次[STA]数据" blockType="command"
    //% STA.shadow="dropdown" STA.options="STA" STA.defl="STA.1"
    export function getRead(parameter: any, block: any) {
        begin()
        let sta = parameter.STA.code;
        if(sta == 1){
            Generator.addCode(`I2C_ib.readIO(I2C_ib.date);`)
        }else if(sta == 2) {
            Generator.addCode(`I2C_ib.readRelay();`)
        }


    }
    //% block="读取数字输出引脚全部状态" blockType="reporter"
    export function getCharRelayData(parameter: any, block: any) {
        begin()
        Generator.addCode(`I2C_ib.readRelayString()`)

    }
    //% block="读取数字输出引脚[IO]" blockType="boolean"
    //% IO.shadow="dropdown" IO.options="IO" IO.defl="IO.0"
    export function relaydate(parameter: any, block: any) {
        let I2Cdate = parameter.IO.code;
        begin()
        Generator.addCode(`I2C_ib.relayDate[${I2Cdate - 1}]`)
    }


    //% block="读取数字输入引脚组[LEN]" blockType="reporter"
    //% LEN.shadow="dropdown" LEN.options="LEN" LEN.defl="LEN.1"
    export function getCharData(parameter: any, block: any) {
        begin()
        let len = parameter.LEN.code;
        Generator.addCode(`I2C_ib.readString(${len})`)

    }
    //% block="读取数字输入引脚[DATE]" blockType="boolean"
    //% DATE.shadow="dropdown" DATE.options="DATE" DATE.defl="DATE.date[0]"
    export function date(parameter: any, block: any) {
        let I2Cdate = parameter.DATE.code;
        begin()
        Generator.addCode(`I2C_ib.date[${I2Cdate}]`)
    }

    //% block="设置数字引脚[IO]输出[LEVEL]" blockType="command"
    //% IO.shadow="dropdown" LEVEL.shadow="dropdown" IO.options="IO" LEVEL.options="LEVEL" IO.defl="IO.0" LEVEL.defl="LEVEL.0"
    export function setWrite (parameter: any, block: any) {
        let io = parameter.IO.code;
        let level = parameter.LEVEL.code;
        begin()
        Generator.addCode(`I2C_ib.write(${Number(io)+Number(level)});`)

    }

    //% block="设置舵机[PWM]角度为[LEVEL]度" blockType="command"
    //% PWM.shadow="dropdown" LEVEL.shadow="range" PWM.options="PWM" PWM.defl="PWM.11" LEVEL.params.min=0 LEVEL.params.max=180    LEVEL.defl=90
    export function servoWrite (parameter: any, block: any) {
        let PWM = parameter.PWM.code;
        let level = parameter.LEVEL.code;
        begin()
        level = level > 180?180:level;
        level = level < 0?0:level;
        Generator.addCode(`I2C_ib.servoWrite(${Number(PWM)},${level});`)

    }

    //% block="设置模拟引脚[PWM]pwm输出[LEVEL]" blockType="command"
    //% PWM.shadow="dropdown" LEVEL.shadow="range" PWM.options="PWM" PWM.defl="PWM.11" LEVEL.params.min=0 LEVEL.params.max=100 LEVEL.defl=100
    export function pwmWrite (parameter: any, block: any) {
        let PWM = parameter.PWM.code;
        let level = parameter.LEVEL.code;
        begin()
        level = level > 100?100:level;
        level = level < 0?0:level;
        Generator.addCode(`I2C_ib.pwmWrite(${Number(PWM)},${level});`)

    }
}
